﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZedGraph;

using Bestcode.MathParser;

namespace NumMethods_HW1_Hydra
{
    public partial class MainForm : Form
    {
        Grid grid;

        double xLeft, xRight;
        int XNumParts;
        
        int TNumParts;
        double stepTao;
        double T;

        int numDumpTimes;
        int currentGraphicTime;

        // ====== Params =======
        double lambda_1;
        double ro_1;
        double k_1;
        double gamma_1;

        double lambda_2;
        double c_2;
        double nv_2;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            groupBoxGraphics.Enabled = false;
        }

        private void numericUpDownGraphicsTime_ValueChanged(object sender, EventArgs e)
        {
            currentGraphicTime = Convert.ToInt32(numericUpDownGraphicsTime.Value);
            grid.DrawGraphics(zedGraphControl_U1, zedGraphControl_U2, currentGraphicTime);
            grid.DrawComparativeGraphic(zedGraphControlCompare, currentGraphicTime);
        }

        private void clearGraphics()
        {
            GraphPane pane_U1 = zedGraphControl_U1.GraphPane;
            GraphPane pane_U2 = zedGraphControl_U2.GraphPane;
            GraphPane pane = zedGraphControlCompare.GraphPane;

            pane_U1.CurveList.Clear();
            zedGraphControl_U1.Invalidate();

            pane_U2.CurveList.Clear();
            zedGraphControl_U2.Invalidate();

            pane.CurveList.Clear();
            zedGraphControl_U2.Invalidate();
        }

        private void buttonRUN_Click(object sender, EventArgs e)
        {
            groupBoxGraphics.Enabled = true;

            xLeft = 0.0;
            xRight = 1.0;
            XNumParts = Convert.ToInt32(numericUpDownXNumStep.Value);

            TNumParts = Convert.ToInt32(numericUpDownTNumStep.Value);
            stepTao = Convert.ToDouble(numericUpDownStepTao.Value);
            numDumpTimes = Convert.ToInt32(numericUpDownNumDumpTimes.Value);
            numericUpDownGraphicsTime.Maximum = numDumpTimes;

            T = TNumParts * stepTao;

            double dumpStep = T / numDumpTimes;

            labelT.Text = T.ToString();
            labelDumpStep.Text = dumpStep.ToString();

            double stepH = (xRight - xLeft) / XNumParts;

            // ============= Params ==============
            lambda_1 = Convert.ToDouble(numericUpDownLambda_1.Value);
            ro_1 = Convert.ToDouble(numericUpDownRo_1.Value);
            k_1 = Convert.ToDouble(numericUpDownK_1.Value);
            gamma_1 = Convert.ToDouble(numericUpDownGamma_1.Value);

            lambda_2 = Convert.ToDouble(numericUpDownLambda_2.Value);
            c_2 = Convert.ToDouble(numericUpDownC_2.Value);
            nv_2 = Convert.ToDouble(numericUpDownNv_2.Value);
            // ===================================

            double lambda_max = lambda_1;
            if (lambda_2 > lambda_max)
                lambda_max = lambda_2;

            if (stepTao < Math.Pow(stepH, 2) / (2.0 * lambda_max))
            {
                grid = new Grid(TNumParts, stepTao, xLeft, xRight, XNumParts, numDumpTimes);
                grid.initParser();
                //grid.initParams(lambda_1, ro_1, k_1, gamma_1, lambda_2, c_2, nv_2);
                grid.initParamsParser(lambda_1, ro_1, k_1, gamma_1, lambda_2, c_2, nv_2, textBox_U1, textBox_U2);
                grid.Calculation();

                groupBoxGraphics.Enabled = true;

                clearGraphics();

                numericUpDownGraphicsTime.Value = numericUpDownNumDumpTimes.Value;
                currentGraphicTime = Convert.ToInt32(numericUpDownGraphicsTime.Value);
                grid.DrawGraphics(zedGraphControl_U1, zedGraphControl_U2, currentGraphicTime);
                grid.DrawComparativeGraphic(zedGraphControlCompare, currentGraphicTime);
            }
            else
            {
                Error er = new Error();
                er.Show();
            }
        }

        private void buttonPlot_Click(object sender, EventArgs e)
        {
            currentGraphicTime = Convert.ToInt32(numericUpDownGraphicsTime.Value);
            grid.DrawGraphics(zedGraphControl_U1, zedGraphControl_U2, currentGraphicTime);
            grid.DrawComparativeGraphic(zedGraphControlCompare, currentGraphicTime);
        }
    }
}
